/**
 *       Copyright (c) 2012 Lukas Zaruba
 *
 *   This file is part of Robot Playground.
 *
 *   Robot Playground is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Robot Playground is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Robot Playground. If not, see <http://www.gnu.org/licenses/>.
 */
package net.zarubsys.robotplayground.dummydevice.modules;

import net.zarubsys.robotplayground.core.Environment;
import net.zarubsys.robotplayground.core.modules.AbstractModule;
import net.zarubsys.robotplayground.core.modules.EmptyModuleConfiguration;
import net.zarubsys.robotplayground.core.modules.annotations.Function;
import net.zarubsys.robotplayground.core.modules.annotations.Param;
import net.zarubsys.robotplayground.dummydevice.DummyDevice;

/**
 * DummyMotionModule
 * 
 * This module takes care about the Device locking properly, there is no need to handle it manually.
 * Notifications are raised once {@link #move(Integer)} or {@link #rotate(Integer)} is called.
 *
 * @author Lukas Zaruba, lukas.zaruba@gmail.com
 */
public class DummyMotionModule extends AbstractModule<DummyDevice, EmptyModuleConfiguration> {
	
	public static final String ID = "dummyMotion";
	
	private DummyDevice device;
	private Environment env;

	@Override
	public void init(DummyDevice device, EmptyModuleConfiguration configuration, Environment env) {
		this.device = device;
		this.env = env;
	}

	@Override
	public void close() {
		// ignore
	}

	@Override
	public String getId() {
		return ID;
	}
	
	@Function(description = "Rotates the dummy device by the given count of degrees (it is not necessary to handle going over 360)")
	public void rotate(@Param(name = "degrees") Integer degrees) {
		device.lockMovementsAccess();
		try {
			int newDir = device.getDirection() + degrees;
			if (newDir < 0) {
				newDir += 360;
			}
			device.setDirection(newDir % 360);
		} finally {
			device.unlockMovementsAccess();
		}
		env.dispatchNotification(this, DummyMotionNotificationCause.ROTATION);
	}
	
	@Function(description = "Moves the dummy device by the given distance in the current direction. It is possible to go backward using values < 0")
	public void move(@Param(name = "distance") Integer distance) {
		double radians = Math.toRadians(device.getDirection());
		int dx = (int) Math.round(Math.sin(radians) * distance);
		int dy = (int) Math.round(Math.cos(radians) * distance);
		device.lockMovementsAccess();
		try {
			device.setX(device.getX() + dx);
			device.setY(device.getY() - dy);
		} finally {
			device.unlockMovementsAccess();
		}
		env.dispatchNotification(this, DummyMotionNotificationCause.MOVE);
	}

	@Override
	public String getRequiredDevice() {
		return DummyDevice.ID;
	}

}

